package com.example.mymvvmdemo.views;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;

import androidx.annotation.Nullable;

public class LikeView extends View {
    private Paint mPaint;
    private Path mPath;
    private TextPaint mTextPaint;
    public boolean isLiked = false;
    private int likeCount = 0;
    private float scale = 1f;
    private float alpha = 1f;
    private float colorProgress = 0f;
    private String likeCountText = "0";
    private Rect textBounds = new Rect();
    private int grayColor = Color.parseColor("#999999");
    private int redColor = Color.parseColor("#FF2442");

    public LikeView(Context context) {
        this(context, null);
    }

    public LikeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LikeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(grayColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPath = new Path();

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(grayColor);
        mTextPaint.setTextSize(30f);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        
        // 绘制心形
        mPath.reset();
        float heartWidth = width * 0.5f;  // 心形宽度为视图宽度的50%
        float heartHeight = height * 0.4f; // 心形高度为视图高度的40%
        float centerX = width / 2f;
        float centerY = height * 0.4f; // 将心形位置上移
        
        // 从心形底部开始绘制
        mPath.moveTo(centerX, centerY + heartHeight / 2);
        
        // 绘制左半边
        mPath.cubicTo(
            centerX, centerY + heartHeight / 4,
            centerX - heartWidth / 2, centerY - heartHeight / 4,
            centerX - heartWidth / 2, centerY - heartHeight / 2
        );
        
        // 绘制左半边顶部
        mPath.cubicTo(
            centerX - heartWidth / 2, centerY - heartHeight * 0.7f,
            centerX - heartWidth / 3, centerY - heartHeight,
            centerX, centerY - heartHeight * 0.8f
        );
        
        // 绘制右半边顶部
        mPath.cubicTo(
            centerX + heartWidth / 3, centerY - heartHeight,
            centerX + heartWidth / 2, centerY - heartHeight * 0.7f,
            centerX + heartWidth / 2, centerY - heartHeight / 2
        );
        
        // 绘制右半边
        mPath.cubicTo(
            centerX + heartWidth / 2, centerY - heartHeight / 4,
            centerX, centerY + heartHeight / 4,
            centerX, centerY + heartHeight / 2
        );
        
        // 计算当前颜色
        int currentColor = getCurrentColor();
        mPaint.setColor(currentColor);
        
        canvas.save();
        canvas.scale(scale, scale, width / 2f, height * 0.4f); // 调整缩放中心点
        mPaint.setAlpha((int) (alpha * 255));
        canvas.drawPath(mPath, mPaint);
        canvas.restore();

        // 绘制点赞数量
        mTextPaint.setColor(currentColor);
        mTextPaint.getTextBounds(likeCountText, 0, likeCountText.length(), textBounds);
        // 调整数字位置，增加与心形的间距
        canvas.drawText(likeCountText, width / 2f, height * 0.8f, mTextPaint);
    }

    private int getCurrentColor() {
        int red = (int) (Color.red(grayColor) + (Color.red(redColor) - Color.red(grayColor)) * colorProgress);
        int green = (int) (Color.green(grayColor) + (Color.green(redColor) - Color.green(grayColor)) * colorProgress);
        int blue = (int) (Color.blue(grayColor) + (Color.blue(redColor) - Color.blue(grayColor)) * colorProgress);
        return Color.rgb(red, green, blue);
    }

    public void setLiked(boolean liked) {
        if (isLiked == liked) return;
        isLiked = liked;
        startAnimation();
    }

    public void setLikeCount(int count) {
        this.likeCount = count;
        this.likeCountText = String.valueOf(count);
        invalidate();
    }

    private void startAnimation() {
        if (isLiked) {
            // 点赞动画
            ObjectAnimator scaleAnim = ObjectAnimator.ofFloat(this, "scale", 1f, 1.2f, 1f);
            scaleAnim.setDuration(300);
            scaleAnim.setInterpolator(new OvershootInterpolator());

            ValueAnimator colorAnim = ValueAnimator.ofFloat(0f, 1f);
            colorAnim.setDuration(300);
            colorAnim.addUpdateListener(animation -> {
                colorProgress = (float) animation.getAnimatedValue();
                invalidate();
            });

            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(scaleAnim, colorAnim);
            animatorSet.start();
        } else {
//            // 取消点赞动画
//            ObjectAnimator scaleAnim = ObjectAnimator.ofFloat(this, "scale", 1f, 0.8f, 1f);
//            scaleAnim.setDuration(300);
//            scaleAnim.setInterpolator(new DecelerateInterpolator());
//
//            ValueAnimator colorAnim = ValueAnimator.ofFloat(1f, 0f);
//            colorAnim.setDuration(300);
//            colorAnim.addUpdateListener(animation -> {
//                colorProgress = (float) animation.getAnimatedValue();
//                invalidate();
//            });
//
//            AnimatorSet animatorSet = new AnimatorSet();
//            animatorSet.playTogether(scaleAnim, colorAnim);
//            animatorSet.start();
        }
    }

    public void setScale(float scale) {
        this.scale = scale;
        invalidate();
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;
        invalidate();
    }
} 